﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Computer_Manager.BUS;
using Computer_Manager.DTO;
using DevExpress.XtraEditors;

namespace Computer_Manager.GUI
{
    public partial class frmEditSubCat : DevExpress.XtraEditors.XtraForm
    {

        DTO_categories dto_cate = new DTO_categories();
        BUS_categories bus_cate = new BUS_categories();
        frmCategories obj = (frmCategories)Application.OpenForms["frmCategories"];
        private int id_cat;

        public int Id_cat
        {
            get { return id_cat; }
            set { id_cat = value; }
        }
        public frmEditSubCat()
        {
            InitializeComponent();
        }

        private void frmEditSubCat_Load(object sender, EventArgs e)
        {
            dto_cate.id_cat = id_cat;
            txtCode.Text = bus_cate.GetByID(dto_cate).Rows[0]["codes_cat"].ToString();
            txtName.Text = bus_cate.GetByID(dto_cate).Rows[0]["name_cat"].ToString();
            dto_cate.parent_id = 0;
            cmbCat.DataSource = bus_cate.GetByParentId(dto_cate);
            cmbCat.DisplayMember = "name_cat";
            cmbCat.ValueMember = "id_cat";
            cmbCat.SelectedValue = bus_cate.GetByID(dto_cate).Rows[0]["parent_id"];
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            dto_cate.id_cat = id_cat;
            txtCode.Text = bus_cate.GetByID(dto_cate).Rows[0]["codes_cat"].ToString();
            txtName.Text = bus_cate.GetByID(dto_cate).Rows[0]["name_cat"].ToString();
            dto_cate.parent_id = 0;
            cmbCat.DataSource = bus_cate.GetByParentId(dto_cate);
            cmbCat.DisplayMember = "name_cat";
            cmbCat.ValueMember = "id_cat";
            cmbCat.SelectedValue = bus_cate.GetByID(dto_cate).Rows[0]["parent_id"];
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtCode.Text == "" || txtName.Text == "")
            {
                if (txtCode.Text == "")
                {
                    txtCode.Properties.NullValuePrompt = "Vui lòng nhập mã loại hàng";
                    txtCode.BackColor = Color.NavajoWhite;
                }
                else
                {
                    txtCode.Properties.NullValuePrompt = "";
                    txtCode.BackColor = Color.White;
                }
                /////////
                if (txtName.Text == "")
                {
                    txtName.Properties.NullValuePrompt = "Vui lòng nhập tên loại hàng";
                    txtName.BackColor = Color.NavajoWhite;
                }
                else
                {
                    txtName.Properties.NullValuePrompt = "";
                    txtName.BackColor = Color.White;
                }
            }
            else
            {
                dto_cate.id_cat = id_cat;
                dto_cate.codes_cat = txtCode.Text;
                dto_cate.name_cat = txtName.Text;
                dto_cate.parent_id = int.Parse(cmbCat.SelectedValue.ToString());
                bus_cate.categories_Update(dto_cate);
                MessageBox.Show("Sửa thành công!!!");
                this.Dispose();
                this.Close();
                obj.GridviewCat();
                obj.GridviewSubCat();
            }
        }
    }
}